﻿import json
import sys
from config import TOP_K, DEFAULT_TABLE
from logs import LOGGER
from milvus_helpers import MilvusHelper
from encode import SentenceModel
import numpy as np

def do_search(table_name: str, question: str, model: SentenceModel, milvus_client: MilvusHelper,embedding:str=None):
    try:
        feat=[]
        if not table_name:
            table_name = DEFAULT_TABLE
        if embedding is None:
            feat = model.sentence_encode([question])
        else:
            feat=np.array([json.loads(embedding)])
        results = milvus_client.search_vectors(table_name, feat, TOP_K)
        vids = [str(x.id) for x in results[0]]
        distances = [x.distance for x in results[0]]
        return vids, distances
    except Exception as e:
        LOGGER.error(f" Error with search : {e}")
        sys.exit(1)
